﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    /// <summary>
    /// Some helpers for List class...
    /// </summary>
    public static class ListHelpers
    {
        public class ListAsChunks<T> : IEnumerable<List<T>>
        {
            int _chunkSize = 1;
            List<T> _items = null;

            public ListAsChunks(int chunkSize, List<T> items)
            {
                _chunkSize = chunkSize;
                _items = items;
            }

            public IEnumerator<List<T>> GetEnumerator()
            {
                List<T> result = new List<T>();
                foreach (var item in _items)
                {
                    result.Add(item);
                    if (result.Count >= _chunkSize)
                    {
                        yield return result;
                        result.Clear();
                    }
                }
                if (result.Count > 0)
                {
                    yield return result;
                }
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
        }

        public static IEnumerable<List<T>> GetListAsChunks<T>(this List<T> l, int chunkSize)
        {
            return new ListAsChunks<T>(chunkSize, l);
        }
    }
}
